package br.com.formulavendas.businessusercase;
// default package

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Endereco;


/**
 	* @author MyEclipse Persistence Tools 
 */
public class ManterEndereco extends BaseBusinessUserCase  
			implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Endereco>,
			br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Endereco> {
	
	public ManterEndereco() {
	}

	private static final Logger log = LoggerFactory.getLogger(ManterEndereco.class);
	
	//property constants
	public static final String LOGRADOURO = "logradouro";
	public static final String NUMERO = "numero";
	public static final String COMPLEMENTO = "complemento";
	public static final String CEP = "cep";
	public static final String CIDADE = "cidade";
	public static final String UF = "uf";

	//Lists
	@Override
	public Endereco findById( java.lang.Integer id) {
        log.debug("getting Endereco instance with id: " + id);
        try {
            Endereco instance = (Endereco) this._session
                    .get("Endereco", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List<Endereco> findByExample(Endereco instance) {
        log.debug("finding Endereco instance by example");
        try {
            List results = this._session
                    .createCriteria("Endereco")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    @Override
	public List<Endereco> findByProperty(String propertyName, Object value) {
      log.debug("finding Endereco instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Endereco as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = this._session.createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List<Endereco> findByLogradouro(Object logradouro
	) {
		return findByProperty(LOGRADOURO, logradouro
		);
	}
	
	public List<Endereco> findByNumero(Object numero
	) {
		return findByProperty(NUMERO, numero
		);
	}
	
	public List<Endereco> findByComplemento(Object complemento
	) {
		return findByProperty(COMPLEMENTO, complemento
		);
	}
	
	public List<Endereco> findByCep(Object cep
	) {
		return findByProperty(CEP, cep
		);
	}
	
	public List<Endereco> findByCidade(Object cidade
	) {
		return findByProperty(CIDADE, cidade
		);
	}
	
	public List<Endereco> findByUf(Object uf
	) {
		return findByProperty(UF, uf
		);
	}
	

	@Override
	public List<Endereco> findAll() {
		log.debug("finding all Endereco instances");
		try {
			String queryString = "from Endereco";
	         Query queryObject = this._session.createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	//CRUD
    @Override
	public void save(Endereco transientInstance) {
        log.debug("saving Endereco instance");
        
        Transaction tx = this._session.beginTransaction();
        try {
            this._session.save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
        	tx.rollback();
            log.error("save failed", re);
            throw re;
        }
    }
    
	@Override
	public void delete(Endereco persistentInstance) {
        log.debug("deleting Endereco instance");
        try {
            this._session.delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }    
	
    public Endereco merge(Endereco detachedInstance) {
        log.debug("merging Endereco instance");
        try {
            Endereco result = (Endereco) this._session
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Endereco instance) {
        log.debug("attaching dirty Endereco instance");
        try {
            this._session.saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Endereco instance) {
        log.debug("attaching clean Endereco instance");
        try {
        	this._session.lock(instance, LockMode.NONE);
          	            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}